package top.hongthorn.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.hongthorn.service.QiniuOSSService;
import top.hongthorn.util.Result;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/oss")
@Api(value = "QiniuOSSController", tags = "QiniuOSSController")
public class QiniuOSSController {

    @Autowired
    private QiniuOSSService qiniuOSSService;

    @ApiOperation(value = "上传图片")
    @PostMapping("/upload")
    public Result upload(@RequestParam(name = "file") MultipartFile file) {
        try {
            return Result.success(200, "上传成功", qiniuOSSService.getFileAskingPath(qiniuOSSService.uploadFile(file)));
        } catch (RuntimeException e) {
            return Result.error(500, e.getMessage(), null);
        }
    }






    @ApiOperation(value = "删除图片")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam String key) {
        Boolean aBoolean = qiniuOSSService.deleteFile(key);
        return aBoolean ? Result.success(200, "文件已删除", aBoolean) : Result.error(500, "删除失败", aBoolean);
    }
}